package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.spa.infrastructure.util.StringUtils;
import com.spa.infrastructure.repository.condition.TicketCondition;
import com.spa.infrastructure.repository.mapper.TicketMapper;
import com.spa.infrastructure.repository.po.TicketPO;
import com.spa.infrastructure.repository.po.UserTicketRefPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class TicketRepositoryService extends MPJBaseServiceImpl<TicketMapper, TicketPO> {

    @Autowired
    private TicketMapper ticketMapper;

    public Long addTicket(TicketPO po) {
        ticketMapper.insert(po);
        return po.getId();
    }

    public void updateTicketById(TicketPO po) {
        update(null, updateWrapper(po));
    }

    public Page<TicketPO> page(TicketCondition condition) {
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper(condition));
    }

    /**
     * 关联查询分页
     *
     * @author: lig
     * @date: 2023/12/26
     */
    public Page<TicketPO> pageRef(TicketCondition condition) {


        MPJLambdaWrapper<TicketPO> wrapper = new MPJLambdaWrapper<TicketPO>("d")
                .selectAll(TicketPO.class)
                .rightJoin(UserTicketRefPO.class,UserTicketRefPO::getTicketId,TicketPO::getId)
                .like(StringUtils.isNotEmpty(condition.getName()), TicketPO::getName, condition.getName())
                .eq(ObjectUtil.isNotNull(condition.getType()), TicketPO::getType, condition.getType())
                .eq(TicketPO::getIsDel, 0)
                .eq(ObjectUtil.isNotNull(condition.getUserId()),UserTicketRefPO::getUserId,condition.getUserId())
                .in(CollUtil.isNotEmpty(condition.getIds()), TicketPO::getId, condition.getIds());


        Page<TicketPO> page =new Page<>();
        page.setSize(condition.getSize());
        page.setCurrent(condition.getCurrent());
        page.setOptimizeJoinOfCountSql(true);
        return selectJoinListPage(page,TicketPO.class, wrapper);

    }


    public List<TicketPO> list(TicketCondition condition) {
        return list(wrapper(condition));
    }


    /**
     * 关联查询
     *
     * @author: lig
     * @date: 2023/12/26
     */
    public List<TicketPO> userTicketList(TicketCondition condition) {

        MPJLambdaWrapper<TicketPO> wrapper = new MPJLambdaWrapper<TicketPO>("d")
                .selectAll(TicketPO.class)
                .selectAs(UserTicketRefPO::getId,"refId")
                .rightJoin(UserTicketRefPO.class,UserTicketRefPO::getTicketId,TicketPO::getId)
                .like(StringUtils.isNotEmpty(condition.getName()), TicketPO::getName, condition.getName())
                .eq(ObjectUtil.isNotNull(condition.getType()), TicketPO::getType, condition.getType())
                .eq(TicketPO::getIsDel, 0)
                .eq(ObjectUtil.isNotNull(condition.getUserId()),UserTicketRefPO::getUserId,condition.getUserId())
                .in(CollUtil.isNotEmpty(condition.getIds()), TicketPO::getId, condition.getIds());


        return selectJoinList(TicketPO.class, wrapper);


    }



    public TicketPO getTicketById(Long id) {
        return getById(id);
    }

    private LambdaQueryWrapper<TicketPO> wrapper(TicketCondition condition) {
        LambdaQueryWrapper<TicketPO> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotEmpty(condition.getName()), TicketPO::getName, condition.getName());
        wrapper.eq(ObjectUtil.isNotNull(condition.getType()), TicketPO::getType, condition.getType());
        wrapper.eq(TicketPO::getIsDel, 0);
        wrapper.in(CollUtil.isNotEmpty(condition.getIds()), TicketPO::getId, condition.getIds());
        return wrapper;
    }

    private LambdaUpdateWrapper<TicketPO> updateWrapper(TicketPO po) {
        LambdaUpdateWrapper<TicketPO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(ObjectUtil.isNotNull(po.getId()), TicketPO::getId, po.getId());
        updateWrapper.set(StringUtils.isNotEmpty(po.getName()), TicketPO::getName, po.getName());
        updateWrapper.set(ObjectUtil.isNotNull(po.getIsDel()), TicketPO::getIsDel, po.getIsDel());
        return updateWrapper;
    }
}
